package com.ksp.api;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.Static;

import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;

public class KSPAPI_UserAgentUtil {

	/**
	 * 返回 "浏览器,操作系统"  组成的字符串
	 * 
	 * @param request
	 * @return
	 */
	public static String getUserAgenInfo(ServletRequest request) {
		HttpServletRequest hreq = (HttpServletRequest)request;
		UserAgent userAgent = UserAgent.parseUserAgentString(hreq.getHeader("User-Agent"));
		Browser browser = userAgent.getBrowser();
		OperatingSystem os = userAgent.getOperatingSystem();
		String wUsrAgeStr = browser.toString() + "," + os.toString();
		System.out.println(wUsrAgeStr);
		return wUsrAgeStr;
	}

	/**
	 * 获取IP
	 * @param req
	 * @return
	 */
	public static String getClientIP(ServletRequest req) {
		HttpServletRequest request = (HttpServletRequest)req;
		String ip = request.getHeader("x-forwarded-for");  
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("Proxy-Client-IP");  
		}  
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("WL-Proxy-Client-IP");  
		}  
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("HTTP_CLIENT_IP");  
		}  
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
		}  
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getRemoteAddr();  
		}  
		return ip; 
	}

}
